package com.dd.cloud.user.service.patient.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.ConsultConstants;
import com.dd.cloud.common.constants.TencentConstants;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.vo.PageReq;
import com.dd.cloud.user.DTO.tencent.ImSendmsgSinParamReq;
import com.dd.cloud.user.entity.consult.ConsultSubscribe;
import com.dd.cloud.user.entity.patient.PatientUserAppraise;
import com.dd.cloud.user.mapper.patient.PatientUserAppraiseMapper;
import com.dd.cloud.user.req.patient.PatientUserAppraiseReq;
import com.dd.cloud.user.res.patient.PatientUserAppraiseRes;
import com.dd.cloud.user.service.consule.IConsultSubscribeService;
import com.dd.cloud.user.service.consule.ITencentImMsgService;
import com.dd.cloud.user.service.patient.IPatientUserAppraiseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户评价表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-07-17
 */
@Service
public class PatientUserAppraiseServiceImpl extends ServiceImpl<PatientUserAppraiseMapper, PatientUserAppraise> implements IPatientUserAppraiseService {

    @Autowired
    private IConsultSubscribeService consultSubscribeService;

    @Autowired
    private ITencentImMsgService tencentImMsgService;

    @Override
    public void addPatientUserAppraise(PatientUserAppraiseReq patientUserAppraiseReq) {
        LambdaQueryWrapper<PatientUserAppraise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PatientUserAppraise::getConsultId, patientUserAppraiseReq.getConsultId());
        Integer count = getBaseMapper().selectCount(wrapper);
        if (count > 0) {
            throw new ZekeException("该问诊订单评价已存在,请勿重复评价");
        }
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.PATIENT).get();
        Integer userId = loginInfo.getId();
        PatientUserAppraise patientUserAppraise = new PatientUserAppraise();
        BeanUtils.copyProperties(patientUserAppraiseReq, patientUserAppraise);
        patientUserAppraise.setUserId(userId);
        boolean flag = this.save(patientUserAppraise);
        if (!flag) {
            throw new ZekeException("保存失败");
        }
        //更新订单状态
        LambdaUpdateWrapper<ConsultSubscribe> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ConsultSubscribe::getStatus, ConsultConstants.Status.SUCC)
                .eq(ConsultSubscribe::getId, patientUserAppraiseReq.getConsultId());
        boolean flag1 = consultSubscribeService.update(updateWrapper);
        if(flag1){
            //评价完成并通知
            ImSendmsgSinParamReq imSendmsgSinParamReq = new ImSendmsgSinParamReq();
            imSendmsgSinParamReq.setFrom_Account(patientUserAppraiseReq.getDoctorId() + "_" + LoginInfo.UserType.DOCTOR);
            imSendmsgSinParamReq.setTo_Account(loginInfo.getId() + "_" + LoginInfo.UserType.PATIENT + "_" + patientUserAppraiseReq.getMedId());
            imSendmsgSinParamReq.setSyncOtherMachine(2);
            imSendmsgSinParamReq.setCloudCustomData(String.valueOf(patientUserAppraiseReq.getConsultId()));
            TencentConstants.ImMsgPojo imMsgPojo = new TencentConstants.ImMsgPojo();
            tencentImMsgService.imSendmsgSymtem(imSendmsgSinParamReq, imMsgPojo.imMsgSet(TencentConstants.ImMsgCustom.EVALUATE));
        }
    }

    @Override
    public IPage<PatientUserAppraiseRes> queryPatientUserAppraise(PageReq pageReq) {
        IPage<PatientUserAppraise> page = pageReq.pagePojo();
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.PATIENT).get();
        IPage<PatientUserAppraiseRes> res = this.getBaseMapper().queryPatientUserAppraise(page, loginInfo.getId());
        return res;
    }
}
